package com.ang.jdk8.stream;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ssang
 * @date 2021-09-26 15:41
 */
public class OthersTest {

    public static void main(String[] args) {
        OthersTest t = new OthersTest();
        t.forEach01();
    }

    public void forEach01(){
        User u1 = new User(1,"A");
        User u2 = new User(2,"B");
        User u3 = new User(3,"C");
        User u4 = new User(4,"D");
        List<User> list = Arrays.asList(u1,u3,u4,u2);

        list = list.stream().distinct().collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    /**
     * distinct，排重
     */
    public void distinct01(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,4,3,2,1);

        List<Integer> newList = list.stream().distinct().collect(Collectors.toList());

        newList.forEach(System.out::println);
    }

    /**
     * removeIf
     * 会改变原有list
     */
    public void removeIf01(){
        User u1 = new User(1,"A");
        User u2 = new User(2,"B");
        User u3 = new User(3,"C");
        User u4 = new User(4,"D");

        List<User> list = new ArrayList<>(Arrays.asList(u1,u2,u3,u4));
        list.forEach(l->{
            System.out.println(l.toString());
        });

        System.out.println("--------------------------");
        list.removeIf(v->(v.getId()==1 || v.name.equals("B")));
        list.forEach(l->{
            System.out.println(l.toString());
        });
    }

    //内部类
    //*****************************************************
    @Data
    @ToString
    class User{
        int id;
        String name;

        public User(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
}
